package com.application.mylibrary.utils;

import android.annotation.SuppressLint;

import android.text.TextUtils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author lixiaoyu
 * @version v1.0
 * @description
 * @since 2017/11/9
 */

public class DateUtils {
    public static final long HOUR = 1000 * 60 * 60;
    public static final long MINUTES = 1000 * 60;
    public static final long ONE_MINUTE = 60000L;
    public static final long ONE_HOUR = 3600000L;
    public static final long ONE_DAY = 86400000L;
    private static final long ONE_WEEK = 604800000L;
    private final static int[] dayArr = new int[]{20, 19, 21, 20, 21, 22, 23,
            23, 23, 24, 23, 22};
//    private final static int[] constellationArr = new int[]{R.string.moxiezuo,
//            R.string.shuipingzuo, R.string.shuangyuzuo, R.string.baiyangzuo,
//            R.string.jinniuzuo, R.string.shuangzizuo, R.string.juxiezuo, R.string.shizizuo, R.string.chunvzuo, R.string.tianpingzuo,
//            R.string.tianxiezuo, R.string.sheshouzuo, R.string.moxiezuo};

    public DateUtils() {
    }

    @SuppressLint("StringFormatInvalid")
    public static String formatBirthday(long timeMillis) {
        if (timeMillis == 0L) {
            return "";
        } else {
            String result = fromatDate(timeMillis, "dd-MM-yyyy");
            return result;
        }
    }

    @SuppressLint("StringFormatInvalid")
    public static String formBirthday(long timeMillis) {
        if (timeMillis == 0L) {
            return "";
        } else {
            String result = fromatDate(timeMillis, "yyyy-MM-dd HH:mm");
            return result;
        }
    }

    public static long getBirthday(String formatText) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
        long beginUseTime = 0L;
        try {
            beginUseTime = sdf.parse(formatText).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return beginUseTime;
        }
        return beginUseTime;
    }

    public static boolean isNow(Long l1, Long l2) {

        if (l2 == 0) {
            return false;
        }

        //当前时间
        Date now = new Date(l1);
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd", Locale.US);
        //获取今天的日期
        String nowDay = sf.format(now);
        //对比的时间
        String day = sf.format(new Date(l2));

        return day.equals(nowDay);


    }

    public static int getYearByTimeStamp(long timeStamp) {
        String date = formatBirthday(timeStamp);
        if (!TextUtils.isEmpty(date)) {
            String year = date.substring(6, 10);
            return Integer.parseInt(year);
        }
        return 0;
    }

    public static int getMonthByTimeStamp(long timeStamp) {
        String date = formatBirthday(timeStamp);
        if (!TextUtils.isEmpty(date)) {
            String month = date.substring(3, 5);
            return Integer.parseInt(month);
        }
        return 0;
    }

    public static int getDayByTimeStamp(long timeStamp) {
        String date = formatBirthday(timeStamp);
        if (!TextUtils.isEmpty(date)) {
            String day = date.substring(0, 2);
            return Integer.parseInt(day);
        }
        return 0;
    }

    @SuppressLint("StringFormatInvalid")
    public static String formatData(long timeMillis, String formatString) {
        if (timeMillis == 0L) {
            return "";
        } else {
            String result = null;
            int targetDay = (int) (timeMillis / 86400000L);
            int nowDay = (int) (System.currentTimeMillis() / 86400000L);
            if (targetDay == nowDay) {
                result = fromatDate(timeMillis, "HH:mm");
            } else if (targetDay + 1 == nowDay) {
//                Context context = PiE.Companion.getPieContext();
//                String formatString = context.getResources().getString(R.string.rc_yesterday_format);
                String s = fromatDate(timeMillis, "HH:mm");
                result = String.format(Locale.US, formatString, s);
            } else {
                int msgYear = getYearByTimeStamp(timeMillis);
                int nowYear = getYearByTimeStamp(System.currentTimeMillis());
                if (nowYear - msgYear > 0) {
                    result = fromatDate(timeMillis, "HH:mm dd/MM/yyyy");
                } else {
                    result = fromatDate(timeMillis, "HH:mm dd/MM");
                }
            }
            return result;
        }
    }

    public static String fromatDate(long timeMillis, String fromat) {
        SimpleDateFormat sdf = new SimpleDateFormat(fromat, Locale.US);
        return sdf.format(new Date(timeMillis));
    }

    public static String getRedTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/ HH:mm:ss ", Locale.US);
        return sdf.format(new Date(time));
    }

    // 设置GMT时间，解决倒计时时间不对问题
    public static String fromatDateGMT(long timeMillis, String fromat) {
        SimpleDateFormat sdf = new SimpleDateFormat(fromat, Locale.US);
        TimeZone gmt = TimeZone.getTimeZone("GMT");
        sdf.setTimeZone(gmt);
        return sdf.format(new Date(timeMillis));
    }

    public static boolean isShowChatTime(long currentTime, long preTime, int interval) {
        int typeCurrent = judgeDate(new Date(currentTime));
        int typePre = judgeDate(new Date(preTime));
        return typeCurrent == typePre ? currentTime - preTime > (long) (interval * 1000) : true;
    }

    @SuppressLint("WrongConstant")
    public static int judgeDate(Date date) {
        Calendar calendarToday = Calendar.getInstance();
        calendarToday.set(11, 0);
        calendarToday.set(12, 0);
        calendarToday.set(13, 0);
        calendarToday.set(14, 0);
        Calendar calendarYesterday = Calendar.getInstance();
        calendarYesterday.add(5, -1);
        calendarYesterday.set(11, 0);
        calendarYesterday.set(12, 0);
        calendarYesterday.set(13, 0);
        calendarYesterday.set(14, 0);
        Calendar calendarTomorrow = Calendar.getInstance();
        calendarTomorrow.add(5, 1);
        calendarTomorrow.set(11, 0);
        calendarTomorrow.set(12, 0);
        calendarTomorrow.set(13, 0);
        calendarTomorrow.set(14, 0);
        Calendar calendarTarget = Calendar.getInstance();
        calendarTarget.setTime(date);
        return calendarTarget.before(calendarYesterday) ? 2014 : (calendarTarget.before(calendarToday) ? 15 : (calendarTarget.before(calendarTomorrow) ? 6 : 2014));
    }

    /**
     * 计算年龄
     *
     * @return <p/>-1 生日format error
     * <p/> -2 生日小于当前时间
     */
    public static int getUserAge(long birthDay) {
        Date date = new Date(birthDay);
        Calendar cal = Calendar.getInstance();
        if (cal.before(date)) {
            return -2;
        }
        int curYear = cal.get(Calendar.YEAR);
        cal.setTime(date);
        int birthYear = cal.get(Calendar.YEAR);
        return curYear - birthYear;
    }

    public static boolean validateBirthday(int birthdayYear) {
        Calendar calendarCur = Calendar.getInstance();
        long time = System.currentTimeMillis();
        calendarCur.setTimeInMillis(time);
        int curYear = calendarCur.get(Calendar.YEAR);
        if (birthdayYear >= curYear - 60 && birthdayYear <= curYear - 12) {
            return true;
        }
        return false;
    }

    public static String formatDay(long timeMillis) {
        if (timeMillis == 0L) {
            return "";
        } else {
            return fromatDate(timeMillis, "dd/MM/yyyy");
        }
    }

    public static String getDateString(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(currentTime);
    }

//    public static String getRecentDate(long visiTime, long systemtime) {
//
//        Calendar calendar = Calendar.getInstance();
//
//        calendar.setTimeInMillis(systemtime);
//        int systemYear = calendar.get(Calendar.YEAR);
//        int systemMonth = calendar.get(Calendar.MONTH);
//        int sysstemDay = calendar.get(Calendar.DAY_OF_MONTH);
//        int systemHour = calendar.get(Calendar.HOUR_OF_DAY);
//        int sysDayYear = calendar.get(Calendar.DAY_OF_YEAR);
//
//        calendar.setTimeInMillis(visiTime);
//        int visitYear = calendar.get(Calendar.YEAR);
//        int visitMonth = calendar.get(Calendar.MONTH);
//        int visitDay = calendar.get(Calendar.DAY_OF_MONTH);
//        int visitHour = calendar.get(Calendar.HOUR_OF_DAY);
//        int vistDayYead = calendar.get(Calendar.DAY_OF_YEAR);
//
//        // 同一天 仅显示几小时之前
//        String msg = PiE.Companion.getResString(R.string.just_now);
//        if (visitYear == systemYear && visitMonth == systemMonth && sysstemDay == visitDay) {
//            int time = systemHour - visitHour;
//            if (time < 1) {
//            } else {
//                String temp = PiE.Companion.getResString(R.string.hours_ago);
//                msg = String.format(Locale.US, temp, time);
//            }
//        } else if (visitYear == systemYear) {
//            int day = sysDayYear - vistDayYead;
//            String temp = PiE.Companion.getResString(R.string.days_ago);
//            msg = String.format(Locale.US, temp, day);
//        } else if (visitYear < systemYear) {
//            long time = systemtime - visiTime;
//            long l = time / ONE_DAY;
//            String temp = PiE.Companion.getResString(R.string.days_ago);
//            msg = String.format(Locale.US, temp, l);
//        }
//        return msg;
//    }
//
//    public static String getUserLastTime(long visiTime, long systemtime) {
//        /**
//         *
//         1、在线：如果能拿到 APP 的心跳，则展示 Online
//         2、距离最后一次活跃时间间隔规则：
//         • 小于1小时，则分钟，例如最后一次活跃时间为58分钟前，则写：58min前，不满一分钟则不计数
//         • 1小时<时间间隔<1天，则写具体小时，例如最后一次活跃时间为3h前，则写：3h前，不满一小时则不计数
//         • 1天<时间间隔<=3天，则写具体天数，例如最后一次活跃时间为2天前，则写：2天前，不满一天则不计数
//         • 时间间隔>3天，则统一显示3天前
//         */
//        String msg = "";
//        //小于1分钟的
//        long time = systemtime - visiTime;
//        if (time <= ONE_MINUTE) {
//            msg = msg = String.format(Locale.US, PiE.Companion.getPieContext().getString(R.string.time_ago), "1min");
//        } else if (time >= ONE_MINUTE && time < ONE_HOUR) {
//            long l = time / ONE_MINUTE;
//            msg = String.format(Locale.US, PiE.Companion.getPieContext().getString(R.string.time_ago), l + "min");
//        } else if (time >= ONE_HOUR && time < ONE_DAY) {
//            long l = time / ONE_HOUR;
//            msg = String.format(Locale.US, PiE.Companion.getPieContext().getString(R.string.time_ago), l + "h");
//        } else if (time >= ONE_DAY && time < ONE_DAY * 3) {
//            long l = time / ONE_DAY;
//            msg = String.format(Locale.US, PiE.Companion.getPieContext().getString(R.string.time_ago), l + "d");
//        } else {
//            msg = String.format(Locale.US, PiE.Companion.getPieContext().getString(R.string.time_ago), "3d");
//        }
//        return msg;
//    }
//
//    //获取自己的星座
//    public static int getXingZuo(long brithday) {
//        int day = getDayByTimeStamp(brithday);
//        int month = getMonthByTimeStamp(brithday);
//        return getConstellation(month, day);
//    }
//
//    /**
//     * Java通过生日计算星座
//     *
//     * @param month
//     * @param day
//     * @return
//     */
//    public static int getConstellation(int month, int day) {
//        int index = (month - 1) >= 0 ? (month - 1) : 0;
//        return day < dayArr[index] ? constellationArr[index]
//                : constellationArr[month];
//    }
//
//    public static String secToTime(long time) {
//        String timeStr = null;
//        long hour = 0;
//        long minute = 0;
//        long second = 0;
//        long millisecond = 0;
//        if (time <= 0)
//            return "00:00:00";
//        else {
//            second = time / 1000;
//            minute = second / 60;
//            millisecond = time % 1000;
//            if (second < 60) {
//
//                timeStr = "00:00:" + unitFormat(second);
//            } else if (minute < 60) {
//                second = second % 60;
//                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
//            } else {//数字>=3600 000的时候
//                hour = minute / 60;
//                minute = minute % 60;
//                second = second - hour * 3600 - minute * 60;
//                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
//            }
//        }
//        return timeStr;
//    }
//
//    public static String secToTime2(long time) {
//        String timeStr ="";
//        long hour ;
//        long minute ;
//        long second ;
//        if (time <= 0)
//            return "00:00";
//        else {
//            second = time / 1000;
//            minute = second / 60;
//            if (second < 60) {
//
//                timeStr = "00:" + unitFormat(second);
//            } else if (minute < 60) {
//                second = second % 60;
//                timeStr = unitFormat(minute) + ":" + unitFormat(second);
//            } else {//数字>=3600 000的时候
//                hour = minute / 60;
//                minute = minute % 60;
//                second = second - hour * 3600 - minute * 60;
//                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
//            }
//        }
//        return timeStr;
//    }
//
//    public static String secTime(long time) {
//        String timeStr = null;
//
//        long second = 0;
//        long minute = 0;
//        if (time <= 0)
//            return "00:00";
//        else {
//            second = time / 1000;
//            minute = second / 60;
//            if (second < 60) {
//                timeStr = "00:" + unitFormat(second);
//            } else if (minute < 60) {
//                second = second % 60;
//                timeStr = unitFormat(minute) + ":" + unitFormat(second);
//            }
//        }
//        return timeStr;
//    }
//
//    public static String secTime(int time) {
//        String timeStr = null;
//        long hour = 0;
//        long second = 0;
//        long minute = 0;
//        if (time <= 0)
//            return "00:00";
//        else {
//            second = time;
//            minute = second / 60;
//            hour = minute / 60;
//            if (second < 60) {
//                timeStr = "00:" + unitFormat(second);
//            } else if (minute < 60) {
//                second = second % 60;
//                timeStr = unitFormat(minute) + ":" + unitFormat(second);
//            } else if (hour < 12) {
//                second = second % 60;
//                minute = minute % 60;
//                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
//            }
//        }
//        return timeStr;
//    }
//
//
//    public static String unitFormat(long i) {//时分秒的格式转换
//        String retStr = null;
//        if (i >= 0 && i < 10)
//            retStr = "0" + Long.toString(i);
//        else
//            retStr = "" + i;
//        return retStr;
//    }
//
//
//    public static String getDay(long time) {
//        long day = time / (24 * 60 * 60 * 1000);
//
//        long newTime = day * (24 * 60 * 60 * 1000);
//        long newtimes = time - newTime;
//        String s = secToTime(newtimes);
//
//        return day + "day " + s;
//
//    }
//
//    public static String showDetailedDate(long time) {
//        Date date = new Date(time);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String format = sdf.format(date);
//        return format;
//    }

}

